<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

use service\DataService;
use service\NodeService;
use think\Db;

/**
 * 打印输出数据到文件
 * @param mixed $data 输出的数据
 * @param bool $force 强制替换
 * @param string|null $file
 */
function p($data, $force = false, $file = null)
{
    is_null($file) && $file = env('runtime_path') . date('Ymd') . '.txt';
    $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . PHP_EOL;
    $force ? file_put_contents($file, $str) : file_put_contents($file, $str, FILE_APPEND);
}
function arr_sort($arr, $field){
    foreach ($arr as $key => $row)
    {
        $volume[$key]  = $row[$field];
    }
    array_multisort($volume, SORT_ASC, $arr);
    return $arr;
}
/**
 * RBAC节点权限验证
 * @param string $node
 * @return bool
 */
function auth($node)
{
    return NodeService::checkAuthNode($node);
}

/**
 * 设备或配置系统参数
 * @param string $name 参数名称
 * @param bool $value 默认是null为获取值，否则为更新
 * @return string|bool
 * @throws \think\Exception
 * @throws \think\exception\PDOException
 */
function sysconf($name, $value = null)
{
    static $config = [];
    if ($value !== null) {
        list($config, $data) = [[], ['name' => $name, 'value' => $value]];
        return DataService::save('SystemConfig', $data, 'name');
    }
    if (empty($config)) {
        $config = Db::name('SystemConfig')->column('name,value');
    }
    return isset($config[$name]) ? $config[$name] : '';
}

/**
 * 日期格式标准输出
 * @param string $datetime 输入日期
 * @param string $format 输出格式
 * @return false|string
 */
function format_datetime($datetime, $format = 'Y年m月d日 H:i:s')
{
    return date($format, strtotime($datetime));
}

/**
 * UTF8字符串加密
 * @param string $string
 * @return string
 */
function encode($string)
{
    list($chars, $length) = ['', strlen($string = iconv('utf-8', 'gbk', $string))];
    for ($i = 0; $i < $length; $i++) {
        $chars .= str_pad(base_convert(ord($string[$i]), 10, 36), 2, 0, 0);
    }
    return $chars;
}

/**
 * UTF8字符串解密
 * @param string $string
 * @return string
 */
function decode($string)
{
    $chars = '';
    foreach (str_split($string, 2) as $char) {
        $chars .= chr(intval(base_convert($char, 36, 10)));
    }
    return iconv('gbk', 'utf-8', $chars);
}

/**
 * 下载远程文件到本地
 * @param string $url 远程图片地址
 * @return string
 */
function local_image($url)
{
    return \service\FileService::download($url)['url'];
}

// 当前的毫秒时间戳
function msectime(){
    $arr = explode(' ', microtime());
    $tmp1 = $arr[0];
    $tmp2 = $arr[1];
    return (float)sprintf('%.0f', (floatval($tmp1) + floatval($tmp2)) * 1000);
}

// 10进制转62进制
function dec62($dec){
    $base = 62;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $ret = '';
    for($t = floor(log10($dec) / log10($base)); $t >= 0; $t--){
        $a = floor($dec / pow($base, $t));
        $ret .= substr($chars, $a, 1);
        $dec -= $a * pow($base, $t);
    }
    return $ret;
}

// 随机字符
function rand_char(){
    $base = 62;
    $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    return $chars[mt_rand(1, $base) - 1];
}

// 密码加密
function encrypt($pass, $salt) {
    if (!$salt) {
        // 8位随机字符串
        $salt = rand_char().dec62(msectime());
    }
    $pass = md5(md5($pass).$salt);
    return array('pass'=>$pass, 'salt'=>$salt);
}
//
//function addReport($num_order=0,$num_user=0,$num_mec=0,$num_set=0,$total_price=0){
//    $time = strtotime('Y--m-d',time());
//    $isexist = Db::name("statistics_order")->where("create_time",$time)->find();
//    if($isexist){
//        $id =  $isexist['id'];
//    }else{
//        $data['create_time'] = $time;
//        Db::name("statistics_order")->insert($data);
//        $id = Db::name('statistics_order')->getLastInsID();
//    }
//    if($num_order){
//        Db::name("statistics_order")->where("id",$id)->setInc("num_order",1);
//    }
//    if($num_user){
//        Db::name("statistics_order")->where("id",$id)->setInc("num_member",1);
//    }
//    if($num_mec){
//        Db::name("statistics_order")->where("id",$id)->setInc("num_mec",$num_mec);
//    }
//    if($num_set){
//        Db::name("statistics_order")->where("id",$id)->setInc("num_set",$num_set);
//    }
//    if($total_price){
//        Db::name("statistics_order")->where("id",$id)->setInc("total_price",$total_price);
//    }
//}